//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 

package org.trafficman.trafficman_main;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;

//import com.google.android.samples.R;
import com.google.googlenav.map.Map;
//import com.meridianhouseplans.jobview2.JobView_Files_NoteEdit.ImageAdapter;

public class TrafficmanFileDownload extends ListActivity {
	private TrafficmanScreens mScreens;
	private EditText mTitleText;
	private EditText mBodyText;
	private int int_intent_active;
	private String export_string;

	private List<String> spin_servers;
	private ArrayAdapter<String> aspnServers;
	private Spinner spnServers;
	private TrafficmanFileServerNotesDbAdapter mDbHelper;
	private TrafficmanFilesNotesDbAdapter files_mDbHelper;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setContentView(R.layout.file_download);

		LinearLayout tmpLayout = (LinearLayout) findViewById(R.id.linearlayout_001);
		TrafficmanConstants.SCREEN_VIEWWIDTH = this.getResources().getDisplayMetrics().widthPixels;
		TrafficmanConstants.SCREEN_VIEWHEIGHT = this.getResources().getDisplayMetrics().heightPixels;
		get_Trafficman_Screen(); // set JobView2 background

		int resID = getResources().getIdentifier(TrafficmanConstants.SCREEN_NAME, null,
				null);
		tmpLayout.setBackground(resID);

		
		TrafficmanFileServerNotesDbAdapter default_Server_mDbHelper = new TrafficmanFileServerNotesDbAdapter(this);
		default_Server_mDbHelper.open();
		default_Server_mDbHelper.getSelectedServer();
		default_Server_mDbHelper.close();
		
		mBodyText = (EditText) findViewById(R.id.jv_file_dl_url);
		mBodyText.setText(TrafficmanConstants.CURRENT_JV_SERVER_URL);
		//mBodyText.setText("http://169.254.70.40/android/db_dl_test");

		mTitleText = (EditText) findViewById(R.id.jv_file_dl_filename);
		mTitleText.setText("Download Testing 1");

		Button confirmButton = (Button) findViewById(R.id.confirm);
		Button cancelButton = (Button) findViewById(R.id.cancel);

		confirmButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				int_intent_active = 1;
				TrafficmanConstants.SAVE_CLICK = 1;
				setResult(RESULT_OK);

				startService(new Intent(TrafficmanFileDownload.this,
						TrafficmanFileDLLocalService.class), null);

				Intent intent = new Intent();
				intent.setClass(TrafficmanFileDownload.this,
						TrafficmanMainActivity.class);
				startActivity(intent);
				finish();
			}
		});

		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				// Constants.CANCEL_CLICK = 1;
				setResult(RESULT_CANCELED);
				Intent intent = new Intent();
				intent.setClass(TrafficmanFileDownload.this,
						TrafficmanMainActivity.class);
				startActivity(intent);
				finish();
			}

		});

		spin_servers = new ArrayList<String>();
		populateFields();

		fillData();

		spnServers = (Spinner) findViewById(R.id.spnServers);
		aspnServers = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spin_servers);
		aspnServers
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnServers.setAdapter(aspnServers);

	}

	private void populateFields() {

		TrafficmanFileServerNotesDbAdapter server_list_mDbHelper = new TrafficmanFileServerNotesDbAdapter(
				this);
		server_list_mDbHelper.open();
		Cursor servers_Cursor = server_list_mDbHelper.fetchAllNotes();
		startManagingCursor(servers_Cursor);

		int numRows = servers_Cursor.count();
		if (numRows > 0) {
			servers_Cursor.first();
			for (int i = 0; i < numRows; ++i) {
				String str_Server_Name = servers_Cursor
						.getString(servers_Cursor
								.getColumnIndex(TrafficmanFileServerNotesDbAdapter.KEY_JV_SERVER));
				spin_servers.add(str_Server_Name);
				servers_Cursor.next();
			} 
		}else {
			String str_spin = getString(R.string.jv_no_server_found);
			spin_servers.add(str_spin);
		}
		server_list_mDbHelper.close();

		
		// this is the default connect to server for a list of JobView2 files.
		startService(new Intent(TrafficmanFileDownload.this,
				TrafficmanFileDLDefaultLocalService.class), null);

		
		//connect to default server
		//get list of available files
		//fillout list of available files
		//select from list
		//download file
		
	}

	private void fillData() {
		// Get all of the rows from the database and create the item list
		files_mDbHelper = new TrafficmanFilesNotesDbAdapter(this);
		files_mDbHelper.open();
		Cursor files_Cursor = files_mDbHelper.fetchAllNotes();
		startManagingCursor(files_Cursor);
		
		String[] from = new String[] { TrafficmanFilesNotesDbAdapter.KEY_JV_TITLE };
		int[] to = new int[] { R.id.text1 };
		SimpleCursorAdapter list = new SimpleCursorAdapter(this,
				R.layout.files_notes_row, files_Cursor, from, to);
		setListAdapter(list);
		files_mDbHelper.close();
	}

	private void get_Trafficman_Screen() {
		mScreens = new TrafficmanScreens(this);
	}

	public class ImageAdapter extends BaseAdapter {

		private Integer[] mThumbIds = { R.drawable.projects_001,
				R.drawable.tasks_003, R.drawable.schedule_01,
				R.drawable.bidding_001, R.drawable.contacts_01 };

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);

			i.setImageResource(mThumbIds[position]);
			i.setAdjustViewBounds(true);
			i.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			i.setBackground(android.R.drawable.picture_frame);
			return i;
		}

		private Context mContext;

	}

	@Override
	protected void onPause() {
		if (int_intent_active == 1) {
			// do nothing
			int_intent_active = 0;
			super.onPause();
		} else {
			super.onPause();
			Intent intent = new Intent();
			intent.setClass(TrafficmanFileDownload.this,
					TrafficmanMainActivity.class);
			startActivity(intent);
			finish();
		}

	}

}
